package com.rapida.theme;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class ThemeManager {
	private static ThemeManager mInst;
	private Context mContext;
	private PackageManager mPackageManager;
	private String mThemePackage;
	private ArrayList<String> mThemes = new ArrayList<String>();
	private OnThemeChangedListener mListener;

	public static ThemeManager get(Context context) {
		if (mInst == null) {
			mInst = new ThemeManager(context);
		}
		return mInst;
	}

	private ThemeManager(Context context) {
		mContext = context;
		mThemePackage = mContext.getPackageName() + ".theme";
		mPackageManager = mContext.getPackageManager();
	}

	public void setOnThemeChangedListener(OnThemeChangedListener listener) {
		mListener = listener;
	}

	public interface OnThemeChangedListener {
		public void OnThemeChanged(String newThemePackageName);
	}

	public void searchTheme() {
		List<PackageInfo> packages = mPackageManager.getInstalledPackages(PackageManager.PERMISSION_GRANTED);
		mThemes.clear();
		for (PackageInfo info : packages) {
			if (info.packageName.startsWith(mThemePackage)) {
				mThemes.add(info.packageName);
			}
		}
	}

	public ArrayList<String> getThemes() {
		return mThemes;
	}

	public void applyTheme(String newThemePackageName) {
		if (mListener != null) {
			mListener.OnThemeChanged(newThemePackageName);
		}
	}
}
